<?php

/**
 * alltosun.com 获取用户的关注列表 friends.php
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 郭莹雪 (guoyx@alltosun.com) $
 * $Date: 2012-2-24 上午11:46:21 $
 * $Id: friends.php 9030 2012-09-20 11:35:27Z liw $
*/

////////////////////////////////////////////////////////////
// @TODO v1表已经不用，v2表更新成connect表 以及采用AnOpenApi工厂函数调用接口 2012-09-20 liw add
////////////////////////////////////////////////////////////

class friends_widget
{
    /**
     * @return string
     */
    public function sinaweibo_friends()
    {
        $site_id = get_site_id('sinaweibo');
        $page = 1;
        $per_page = 100;
        $limit = 'LIMIT '.(($page - 1) * $per_page).','.$per_page;
        $user_list = _model('connect')->getList(array('connect_site_id'=>$site_id), $limit);
        while (!empty($user_list)) {
            foreach ($user_list as $v) {
                $_SESSION['sinaweibo']['last_key']['oauth_token'] = $v['oauth_token'];
                $_SESSION['sinaweibo']['last_key']['oauth_token_secret'] = $v['oauth_token_secret'];
                $_SESSION['user_id'] = $v['user_id'];
                $cursor = -1;
                while ($cursor != 0) {
                    $friends_list = AnSinaweibo::getFriendsList($cursor, 200, $v['connect_user_id']);
                    if (!empty($friends_list['error_code'])) {
                        mail('guoyx@alltosun.com', 'sinaweibo', $v['user_id'].var_export($friends_list, true));
                        break;
                    }
                    foreach ($friends_list['users'] as $v1) {
                        // 插入数据库
                        $info = array(
                            'user_id' => $v['connect_user_id'],
                            'friend_id' => $v1['id'],
                            'screen_name' => $v1['screen_name'],
                            'pinyin' => Pinyin($v1['screen_name'], 1),
                            'profile_image_url' => $v1['profile_image_url'],
                            'gender' => $v1['profile_image_url']
                        );
                        _model('friends_sinaweibo')->create($info, 'REPLACE');
                        usleep(100);
                    }
                    $cursor = $friends_list['next_cursor'];
                    usleep(100);
                }
                usleep(100);
            }
            $page++;
            $limit = 'LIMIT '.(($page - 1) * $per_page).','.$per_page;
            $user_list = _model('connect_v1')->getList(array('connect_site_id'=>$site_id), $limit);
            usleep(100);
        }
        return "ok";
    }

    public function qqweibo_friends()
    {
        $site_id = get_site_id('qqweibo');
        $reqnum = 200;
        $startindex = 0;
        $page = 1;
        $per_page = 100;
        $limit = 'LIMIT '.(($page - 1) * $per_page).','.$per_page;
        $user_list = _model('connect_qq_v1')->getList(array('1'=>1), $limit);
        while (!empty($user_list)) {
            foreach ($user_list as $v) {
                $_SESSION['qqweibo']['last_key']['oauth_token'] = $v['oauth_token'];
                $_SESSION['qqweibo']['last_key']['oauth_token_secret'] = $v['oauth_token_secret'];
                $_SESSION['qqweibo']['last_key']['open_id'] = $v['connect_open_id'];
                $_SESSION['tencent_oauth_token_secret'] = $v['oauth_token_secret'];
                $_SESSION['tencent_access_token'] = $v['oauth_token'];
                $_SESSION['user_id'] = $v['user_id'];
                var_dump('ss', $_SESSION);
                $hasnext = 0;
                $page_no = 1;
                while ($hasnext == 0) {
                    $startindex = $reqnum * ($page_no - 1);
                    $friends_list = AnQqweibo::getFriendsList($startindex, $reqnum);
                    if ($friends_list['errcode'] != 0) {
                        mail('guoyx@alltosun.com', 'qqweibo', $v['user_id'].var_export($friends_list, true).var_export($_SESSION, true));
                        break;
                    }
                    foreach ($friends_list['data']['info'] as $v1) {
                        $info = array(
                            'name' => $v['connect_user_name'],
                            'friend_name' => $v1['name'],
                            'pinyin' => Pinyin($v1['name'], 1),
                            'openid' => $v1['openid'],
                            'nick' => $v1['nick'],
                            'head' => $v1['head'],
                            'sex' => $v1['sex']
                        );
                        _model('friends_qqweibo')->create($info, 'REPLACE');
                        usleep(100);
                    }
                    $page_no++;
                    $hasnext = $friends_list['data']['hasnext'];
                    usleep(100);
                }
                unset($_SESSION['qqweibo']);
                unset($_SESSION['tencent_oauth_token_secret']);
                unset($_SESSION['tencent_access_token']);
                unset($_SESSION['user_id']);
                usleep(100);
            }
            $page++;
            $limit = 'LIMIT '.(($page - 1) * $per_page).','.$per_page;
            $user_list = _model('connect_qq_v1')->getList(array('1'=>1), $limit);
            usleep(100);
        }
        return "ok";
    }

    public function renren_friends()
    {
        $site_id = get_site_id('renren');
        $step = 200;
        $per_page = 100;
        $page = 1;
        $limit = 'LIMIT '.(($page - 1) * $per_page).','.$per_page;
        $user_list = _model('connect_v2')->getList(array('connect_site_id'=>$site_id), $limit);
        while (!empty($user_list)) {
            foreach ($user_list as $v) {
                $start = 0;
                $end = $step;
                $page = 1;
                if (time() > $v['expires_time']) {
                    refresh_login($v['user_id'], 'renren');
                } else {
                    $_SESSION['renren']['token']['access_token'] = $v['access_token'];
                }
                $_SESSION['user_id'] = $v['user_id'];
                $friends_list = AnRenren::getFriendsList($start, $end);
                if (isset($friends_list['error_msg'])) {
                     mail('guoyx@alltosun.com', 'renren', $v['user_id'].var_export($friends_list));
                     break;
                }
                while (!empty($friends_list)) {
                    foreach ($friends_list as $v1) {
                        $info = array(
                            'user_id' => $v['connect_user_id'],
                            'friend_id' => $v1['id'],
                            'tinyurl' => $v1['tinyurl'],
                            'sex' => $v1['sex'],
                            'name' => $v1['name'],
                            'pinyin' => Pinyin($v1['name'], 1),
                            'headurl' => $v1['headurl']
                        );
                        _model('friends_renren')->create($info, 'REPLACE');
                        usleep(100);
                    }
                    $page++;
                    $start = ($page - 1)*$step;
                    $end = $start + $step;
                    $friends_list = AnRenren::getFriendsList($start, $end);
                    usleep(100);
                }
                unset($_SESSION['renren']['token']);
                unset($_SESSION['user_id']);
                usleep(100);
            }
            $page++;
            $limit = 'LIMIT '.(($page - 1) * $per_page).','.$per_page;
            $user_list = _model('connect_v2')->getList(array('connect_site_id'=>$site_id), $limit);
            usleep(100);
        }
        return "ok";
    }

    public function kaixin_friends()
    {
        $site_id = get_site_id('kaixin');
        $num = 50;
        $per_page = 100;
        $page = 1;
        $limit = 'LIMIT '.(($page - 1) * $per_page).','.$per_page;
        $user_list = _model('connect_v2')->getList(array('connect_site_id'=>$site_id), $limit);
        while (!empty($user_list)) {
            foreach ($user_list as $v) {
                var_dump($v);
                $_SESSION['kaixin']['token']['access_token'] = $v['access_token'];
                $_SESSION['user_id'] = $v['user_id'];
                $start = 0;
                $page = 1;
                $list = AnKaixin::getFriendsList('', $start, $num);
                if (isset($list['response']['error_code'])) {
                     mail('guoyx@alltosun.com', 'kaixin', $v['user_id'].var_export($list, true).var_export($_SESSION, true));
                     break;
                }
                while (!empty($list['response']['users'])) {
                    foreach ($list['response']['users'] as $v1) {
                        $info = array(
                            'user_id' => $v['connect_user_id'],
                            'friend_id' => $v1['uid'],
                            'name' => $v1['name'],
                        	'pinyin' => Pinyin($v1['name'], 1),
                            'gender' => $v1['gender'],
                            'logo50' => $v1['logo50']
                        );
                        _model('friends_kaixin')->create($info, 'REPLACE');
                        usleep(10);
                    }
                    $page++;
                    $start = ($page - 1)*$num;
                    $list = AnKaixin::getFriendsList('', $start, $num);
                    usleep(100);
                }
                unset($_SESSION['renren']['token']);
                unset($_SESSION['user_id']);
                usleep(100);
            }
            $page++;
            $limit = 'LIMIT '.(($page - 1) * $per_page).','.$per_page;
            $user_list = _model('connect_v2')->getList(array('connect_site_id'=>$site_id), $limit);
            usleep(100);
        }
        return "ok";
    }
}
?>